package com.dawn.seckilldemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dawn.seckilldemo.exception.GlobalException;
import com.dawn.seckilldemo.mapper.UserMapper;
import com.dawn.seckilldemo.pojo.User;
import com.dawn.seckilldemo.service.UserService;
import com.dawn.seckilldemo.utils.CookieUtil;
import com.dawn.seckilldemo.utils.MD5Util;
import com.dawn.seckilldemo.utils.UUIDUtil;
import com.dawn.seckilldemo.vo.LoginVo;
import com.dawn.seckilldemo.vo.RespBean;
import com.dawn.seckilldemo.vo.RespBeanEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dawnfades
 * @since 2021-09-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //登录  id就是手机号
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //判断是否为空 参数校验
//        if(StringUtils.isEmpty(mobile)
//                || StringUtils.isEmpty(password)){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        //手机校验
//        if(!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        //查询数据库
        User user = userMapper.selectById(mobile);
        if(user == null){
           throw new GlobalException(RespBeanEnum.LOGIN_ERROR);//抛异常让全局处理
        }
        if(!MD5Util.fromPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();

//        request.getSession().setAttribute(ticket,user);
        //为了实现分布式session,把session放在redis里面
        redisTemplate.opsForValue().set("user:"+ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request,HttpServletResponse response) {
        if(StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user =(User) redisTemplate.opsForValue().get("user:" + userTicket);
        //如果用户不为null,重新设置cookie
        if(user != null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }

    //更改密码--引出问题：如果用户信息改变，怎么同步redis和数据库的信息
    //更改密码，删除该用户的再redis中的信息，
    @Override
    public RespBean updatePassword(String userTicket,String password,HttpServletRequest request,HttpServletResponse response) {
        User user = getUserByCookie(userTicket, request, response);

        if(user == null){
            throw  new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSlat()));
        int i = userMapper.updateById(user);
        if(i==1){
            //删除redis
            redisTemplate.delete("user:"+userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }

}
